export class DateUtil {

  /*
  根据时间戳获取距离当前月数
   */
  public static getMonth(date: number): string {
    const now = new Date().getTime();
    let dayStr;
    let days = (now - date) / 1000 / 3600 / 24 / 365;

    if (days < 1) {
      days = days * 12;
      if (days < 1) {
        days = days / 12 * 365;
        dayStr = days.toFixed(0) + '天';
      } else {
        dayStr = days.toFixed(0) + '月多';
      }
    } else {
      dayStr = days.toFixed(0) + '年多';
    }


    return dayStr;
  }


  public static reqDate(startDate: Date): string {

    return startDate.getFullYear()
      + '_' + this.number0(startDate.getMonth() + 1)
      + '_' + this.number0(startDate.getDate())
      + '_' + this.number0(startDate.getHours())
      + '_' + this.number0(startDate.getMinutes())
      + '_' + this.number0(startDate.getSeconds());
  }

  public static date2str(startDate: Date): string {

    return startDate.getFullYear()
      + '-' + this.number0(startDate.getMonth() + 1)
      + '-' + this.number0(startDate.getDate())
      + 'T' + this.number0(startDate.getHours())
      + ':' + this.number0(startDate.getMinutes())
      + ':' + this.number0(startDate.getSeconds());
  }

  public static date2strEnd(startDate: Date): string {

    return startDate.getFullYear()
      + '-' + this.number0(startDate.getMonth() + 1)
      + '-' + this.number0(startDate.getDate())
      + 'T23:59:59';
  }

  public static date2strStart(startDate: Date): string {

    const now = new Date();

    return startDate.getFullYear()
      + '-' + this.number0(startDate.getMonth() + 1)
      + '-' + this.number0(startDate.getDate())
      + 'T' + this.number0(now.getHours())
      + ':' + this.number0(now.getMinutes())
      + ':' + this.number0(now.getSeconds());
  }

  public static date2strYMD(startDate: Date): string {
    return startDate.getFullYear()
      + '-' + this.number0(startDate.getMonth() + 1)
      + '-' + this.number0(startDate.getDate());
  }

  /**
   * 小于10补0
   */
  public static number0(numb: number): string {
    if (numb < 10 && numb > -1) {
      return '0' + numb;
    }
    return numb + '';
  }


  public static getStart(date: Date): Date {
    const result = new Date(date.getFullYear(), date.getMonth(), date.getDate());
    result.setHours(0, 0, 0, 0);
    return result;
  }

  public static getMonthStart(date: Date): Date {
    const result = new Date(date.getFullYear(), date.getMonth(), 1);
    result.setHours(0, 0, 0, 0);
    return result;
  }

  public static getMonthEnd(date: Date): Date {
    const result = new Date(date.getFullYear(), date.getMonth() + 1, 1);
    result.setHours(0, 0, 0, 0);
    const endDate = new Date(result.getTime() - 1000);
    return endDate;
  }

  public static isWorkDay(dateStamp: number): boolean {
    switch (new Date(dateStamp).getDay()) {
      default:
      case 0:
      case 6:
        return false;
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
        return true;

    }
  }


  public static addZero(date: number): string {
    return (date > 9 ? date : '0' + date) + '';
  }


  public static getWeek(year: number, month: number, day: number): string {
    const date = new Date(year, month - 1, day);
    return this.getWeekByDate(date);
  }

  public static getWeekByDate(date: Date): string {
    switch (date.getDay()) {
      default:
        return '未知';
      case 0:
        return '周日';
      case 1:
        return '周一';
      case 2:
        return '周二';
      case 3:
        return '周三';
      case 4:
        return '周四';
      case 5:
        return '周五';
      case 6:
        return '周六';
    }
  }


  public static time2txt(time: number): string {
    const now = new Date().getTime();
    const day = new Date(time);
    const t = ((now - time) / 1000);
    console.log(t);
    if (t < 60) {
      return '刚刚';
    } else if (t < 3600) {
      return (t / 60).toFixed(0) + '分钟前';
    } else if (t < 86400) {
      return (t / 3600).toFixed(0) + '小时前';
    } else if (day.getMonth() === new Date().getMonth()) {
      return (t / 86400).toFixed(0) + '天前';
    } else {
      return this.date2str(new Date(time));
    }
  }

}
